﻿
Imports System.Runtime.CompilerServices
Imports osi.root.connector
Imports osi.root.utils

Public Interface encryptor
    Function encrypt(ByVal key() As Byte, ByVal i() As Byte, ByRef o() As Byte) As Boolean
    Function decrypt(ByVal key() As Byte, ByVal i() As Byte, ByRef o() As Byte) As Boolean
End Interface

Namespace encrypt
    Public Module _encryptor
        <Extension()> Public Function bypass(ByVal e As encryptor) As Boolean
            Return assert(Not e Is Nothing) AndAlso
                   object_compare(e, osi.service.secure.encrypt.bypass.instance) = 0
        End Function

        <Extension()> Public Function bypass2(ByVal e As encryptor) As Boolean
            Return assert(Not e Is Nothing) AndAlso
                   object_compare(e, osi.service.secure.encrypt.bypass2.instance) = 0
        End Function

        <Extension()> Public Function encrypt(ByVal e As encryptor,
                                          ByVal key() As Byte,
                                          ByVal i() As Byte) As Byte()
            Dim o() As Byte = Nothing
            assert(Not e Is Nothing)
            assert(e.encrypt(key, i, o))
            Return o
        End Function

        <Extension()> Public Function decrypt(ByVal e As encryptor,
                                          ByVal key() As Byte,
                                          ByVal i() As Byte) As Byte()
            Dim o() As Byte = Nothing
            assert(Not e Is Nothing)
            assert(e.decrypt(key, i, o))
            Return o
        End Function

        <Extension()> Public Function encrypt(ByVal e As encryptor,
                                          ByVal key As String,
                                          ByVal i() As Byte,
                                          ByRef o() As Byte) As Boolean
            If e Is Nothing Then
                Return False
            Else
                Return e.encrypt(str_bytes(key), i, o)
            End If
        End Function

        <Extension()> Public Function decrypt(ByVal e As encryptor,
                                          ByVal key As String,
                                          ByVal i() As Byte,
                                          ByRef o() As Byte) As Boolean
            If e Is Nothing Then
                Return False
            Else
                Return e.decrypt(str_bytes(key), i, o)
            End If
        End Function

        <Extension()> Public Function encrypt(ByVal e As encryptor,
                                          ByVal key As String,
                                          ByVal i() As Byte) As Byte()
            Dim o() As Byte = Nothing
            assert(e.encrypt(key, i, o))
            Return o
        End Function

        <Extension()> Public Function decrypt(ByVal e As encryptor,
                                          ByVal key As String,
                                          ByVal i() As Byte) As Byte()
            Dim o() As Byte = Nothing
            assert(e.decrypt(key, i, o))
            Return o
        End Function
    End Module
End Namespace
